package com.sh.data.engine.domain.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.data.engine.common.util.DateUtil;
import com.sh.data.engine.common.util.DateUtil.DATE_PATTERN;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.domain.api.model.domain.DataApiRecordSummaryDomain;
import com.sh.data.engine.domain.api.model.domain.DataApiReqTrendSummaryDomain;
import com.sh.data.engine.domain.api.service.DataApiCallStatisticsService;
import com.sh.data.engine.repository.dao.api.DataApiCallStatisticsServiceMapper;
import com.sh.data.engine.repository.dao.api.entity.DataApiCallStatisticsEntity;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DataApiCallStatisticsServiceImpl
    extends ServiceImpl<DataApiCallStatisticsServiceMapper, DataApiCallStatisticsEntity>
    implements DataApiCallStatisticsService {

    @Override
    public List<DataApiReqTrendSummaryDomain> getDataApiReqTrendSummary(
        String startDate, String endDate, Long projectId, String userId) {
        LambdaQueryWrapper<DataApiCallStatisticsEntity> eq =
            new LambdaQueryWrapper<DataApiCallStatisticsEntity>()
                .eq(DataApiCallStatisticsEntity::getProjectId, projectId);
        Date start = DateUtil.parse(startDate, DATE_PATTERN.yyyyMMdd);
        Date end = DateUtil.parse(endDate, DATE_PATTERN.yyyyMMdd);

        QueryWrapper<DataApiCallStatisticsEntity> dataApiCallStatisticsEntityQueryWrapper =
            new QueryWrapper<DataApiCallStatisticsEntity>()
                .select("statistics_date as statisticsDate , sum(total_count) as count")
                .eq("project_id", projectId)
                .ge("statistics_date", start)
                .le("statistics_date", end)
                .groupBy("statistics_date");
        if (Objects.nonNull(userId)) {
            dataApiCallStatisticsEntityQueryWrapper.eq("user_id", userId);
        }
        List<Map<String, Object>> maps = listMaps(dataApiCallStatisticsEntityQueryWrapper);

        List<DataApiReqTrendSummaryDomain> r1 = Lists.newArrayList();
        for (Map<String, Object> p : maps) {
            Object statisticsdate = p.get("statisticsdate");
            if (null == statisticsdate) {
                statisticsdate = p.get("STATISTICSDATE");
            }
            if (null == statisticsdate) {
                statisticsdate = p.get("statisticsDate");
            }
            Date statisticsDate = (Date) statisticsdate;
            Object count1 = p.get("count");
            if (count1 == null) {
                count1 = p.get("COUNT");
            }
            long count = new BigDecimal(count1.toString()).longValue();
            DataApiReqTrendSummaryDomain build =
                DataApiReqTrendSummaryDomain.builder()
                    .statisticsDate(statisticsDate)
                    .count(count)
                    .build();
            r1.add(build);
        }
        return r1;
    }

    @Override
    public DataApiRecordSummaryDomain getDataApiRecordSummaryDomain() {

        Long projectId = ServletUtils.getProjectId();
        Date date = new Date();
        Date todayStart = DateUtil.getTodayStart(date);

        List<DataApiCallStatisticsEntity> list = this.list(
            new LambdaQueryWrapper<>(DataApiCallStatisticsEntity.class).eq(
                    DataApiCallStatisticsEntity::getStatisticsDate, todayStart)
                .eq(DataApiCallStatisticsEntity::getProjectId, projectId));

        Long successTotal = 0L;
        Long failTotal = 0L;

        for (DataApiCallStatisticsEntity dataApiCallStatisticsEntity : list) {
            successTotal = successTotal + dataApiCallStatisticsEntity.getSuccCount();
            failTotal = failTotal = dataApiCallStatisticsEntity.getFailCount();
        }
        DataApiRecordSummaryDomain dataApiRecordSummaryDomain = new DataApiRecordSummaryDomain();
        dataApiRecordSummaryDomain.setFailTotal(failTotal);
        dataApiRecordSummaryDomain.setSuccessTotal(successTotal);

        return dataApiRecordSummaryDomain;
    }
}
